package com.fastloan.mgt.application.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.common.model.CheckResult;
import com.fastloan.core.apiauth.base.service.impl.ry.RYEncryptMode;
import com.fastloan.core.apiauth.base.service.impl.ry.model.RYRequestParam;
import com.fastloan.core.apiauth.base.util.AesUtil;
import com.fastloan.core.apiauth.base.util.RsaUtil;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.contract.model.ApplicationLoanResult;
import com.fastloan.mgt.application.contract.service.LoanCallback;
import com.fastloan.mgt.jx.model.dto.JxLoanDTO;
import com.fastloan.mgt.jx.model.vo.JxLoanResultVO;
import com.fastloan.mgt.jx.service.JxService;
import com.fastloan.mgt.verify.enums.ApiStore;
import com.fastloan.mgt.verify.util.ApiCall;
import com.fastloan.mgt.verify.util.ApiUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import static com.fastloan.mgt.verify.util.ApiUtil.KX_SUCCESS_CODE;

/**
 * @author yanwe
 * createTime 2018-01-2018/1/9 16:27
 */
@Service
public class LoanCallbackImpl implements LoanCallback {
    @Autowired
    private ApplicationService applicationService;

    private static final String puk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgTrwHRehDL1eS+rX28iZMG9qNRpRPtE0hoGNJkZ7OpmTDtREk6TZ+5FOTVeP/QOQt+7Gthu2XHbHrAQc6q2x0CRUro6nZniv83uUJMABAVgxUju56MXbxc731nfwyPTG2BMvenN+DwDty9voK1D8RSpAl1N/KNQm1LGB0uHgDWwIDAQAB";

    private static final String key = "p1clFujUN8jkQvrTUe7HWrwFUjEvq5yh";

    @Autowired
    private JxService jxService;


    @Override
    public void loanCallback(ApplicationLoanResult loanResult) {

        CheckResult checkResult = jxService.backLoanResult(loanResult.getApplicationId());
        if (checkResult.isCheckResult()) {
            Map checkResultData = (Map) checkResult.getData();
            JxLoanResultVO jxLoanResultVO = (JxLoanResultVO) checkResultData.get("jxLoanResultVO");
            JxLoanDTO jxLoanDTO = (JxLoanDTO) checkResultData.get("jxLoanDTO");
            RYRequestParam ryRequestParam = new RYRequestParam();
            RYEncryptMode encryptBean = new RYEncryptMode();
            encryptBean.setRsaPubKey(puk);
            String bean = encryptBean.encryptBean(jxLoanResultVO, key);
            ryRequestParam.setData(bean);
            ryRequestParam.setIv(encryptBean.getAesIv());
            ryRequestParam.setSc(encryptBean.getEncryptAesKey());
            ApiUtil.sendJsonAsync(JSON.toJSONString(ryRequestParam), jxLoanDTO.getLoan_url()
                    , this::reCall
                    , (apiCall, apiResult) -> {
                        String apiResultData = apiResult.getData();
                        String decryptData = AesUtil.decryptByCBC(apiResultData, encryptBean.getAesKey(), encryptBean.getAesIv());
                        JSONObject jsonObject = JSON.parseObject(decryptData);
                        String sign = encryptBean.generateSign(key, jsonObject.getString("rand"));
                        if (sign.equals(jsonObject.getString("sign"))) {
                            String code = jsonObject.getString("code");
                            code = code.toUpperCase();
                            if (!code.equals("SUCCESS")) {
                                reCall(apiCall);
                            }
                        }
                    });
        }
    }

    private void reCall(ApiCall apiCall) {
        try {
            Thread.sleep(300 * 1000);//5分钟重试一次
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        apiCall.reCall();
    }

}
